<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd"
          style="margin-right: 5px">新增</a-button>
        <ExcelButton :config="excelConfig"></ExcelButton>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <ExtLdpDrawer @register="registerExtLdpDrawer" @success="reload" showFooter/>
    <LdpConnectListDrawer @register="registerLdpConnectDrawer"/>
    <ExtLdpDetail @register="registerExtLdpDetail"/>

  </div>
</template>
<script lang="ts" name="ext-ldp-template-list" setup>
import { ref } from 'vue';
import { BasicTable, TableAction, ActionItem } from '/@/components/Table';
import LdpConnectListDrawer from './LdpConnectListDrawer.vue';
import ExtLdpDetail from './ExtLdpDetail.vue';
import { useDrawer } from '/@/components/Drawer';
import { pageLdp, deleteLdp, batchDeleteLdp, getExportUrl, getImportUrl } from './api';
import { columns, searchFormSchema } from './data';
import ExtLdpDrawer from './ExtLdpDrawer.vue';
import { useMessage } from "/@/hooks/web/useMessage";
import { useListPage } from '/@/hooks/system/useListPage'
import ExcelButton from '/@/components/My/ExcelButton.vue'

const [registerLdpConnectDrawer, { openDrawer: openConnectionTmplDrawer }] = useDrawer();
const [registerExtLdpDrawer, { openDrawer: openExtLdpDrawer }] = useDrawer();
const [registerExtLdpDetail, { openDrawer: openExtLdpDetail }] = useDrawer();
const { createMessage } = useMessage();

const excelConfig = {
  exportConfig: {
    name: "身份源",
    url: () => getExportUrl,
  },
  importConfig: {
    url: () => getImportUrl
  }
}

const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  designScope: 'ext-ldp-list',
  tableProps: {
    title: '身份源码列表',
    bordered: false,
    api: pageLdp,
    actionColumn: {
      width: 110,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
    useSearchForm: true,
    columns: columns,
    formConfig: {
      schemas: searchFormSchema
    }
  }
})
const [registerTable, { reload }, { selectedRowKeys }] = tableContext;

/**
 * 操作列定义
 * @param record
 */
function getTableAction(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    },
  ]
}


const handleOpenConnectionTmpls = (record) => {
  openConnectionTmplDrawer(true, { record })
}

/**
 * 租户详情
 */
const handleExtLdpDetail = (record) => {
  openExtLdpDetail(true, { record });
}

/**
 * 下拉操作栏
 */
 function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '删除',
      ifShow: () => record.id !== '1',
      popConfirm: {
        title: `是否确认删除 ${record.name}`,
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '详情',
      onClick: handleExtLdpDetail.bind(null, record),
    }, 

    {
      label: '登录模式',
      onClick: handleOpenConnectionTmpls.bind(null, record),
    }, 
  ]
}


/**
 * 新增事件
 */
function handleAdd() {
  openExtLdpDrawer(true, {
    isUpdate: false,
  });
}

/**
 * 编辑事件
 */
function handleEdit(record) {
  openExtLdpDrawer(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteLdp({ id: record.id }, reload);
}

/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDeleteLdp({ ids: selectedRowKeys.value }, reload);
}
</script>
